package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeServiceImp implements UserLikeService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override//添加用户喜欢
    public void save(UserLike userLike) {
        //创建条件查询表示是否已经存在喜欢记录
        Query query = new Query(Criteria.where("userId").is(userLike.getUserId())
                .and("likeUserId").is(userLike.getLikeUserId()));
        //不存在的话 添加用户喜欢
        if (!mongoTemplate.exists(query, UserLike.class)) {
            mongoTemplate.save(userLike);
        }
    }

    @Override//判断是否相互喜欢
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        //创建条件我喜欢他
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        //调用mongo判断是否存在记录
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        //创建条件他喜欢我
        Query query1 = new Query(Criteria.where("userId").is(likeUserId)
                .and("likeUserId").is(userId));
        //调用mongo判断是否存在记录
        boolean exists1 = mongoTemplate.exists(query, UserLike.class);
        //返回结果
        return exists && exists1;
    }

    @Override//删除用户喜欢
    public void remove(Long userId, Long likeUserId) {
        //调用rpc根据用户id和被喜欢的id删除
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    @Override//查询用户相互喜欢、喜欢、粉丝数量
    public UserLikeCountVo findUserLikeCountVo(Long userId) {
        //相互喜欢: 查询好友列表 (  从`friend`表中根据`userId`查询我的所有好友数量  )
        long eachLoveCount = mongoTemplate.count(new Query(Criteria.where("userId").is(userId)), Friend.class);
        //喜欢: 查询你喜欢的用户 (  从`user_like`表中根据`userId`查询所有我喜欢的用户数量  )
        long loveCount = mongoTemplate.count(new Query(Criteria.where("userId").is(userId)), UserLike.class);
        //粉丝: 查询别人喜欢你的用户 (  从`user_like`表中根据`likeUserId`查询所有喜欢我的用户数量 )
        long fanCount = mongoTemplate.count(new Query(Criteria.where("likeUserId").is(userId)), UserLike.class);
        //返回vo
        return new UserLikeCountVo(eachLoveCount,loveCount,fanCount);
    }

    @Override//根据类型查询用户相互喜欢、喜欢、粉丝的用户详情
    public PageBeanVo findUserLikeVoByType(Long userId, Integer type, Integer pageNum, Integer pageSize) {
        //创建分页索引
        Integer index=(pageNum-1)*pageSize;
        //创建vo集合
        List<UserLikeVo> userLikeVos=new ArrayList<>();
        //根据类型判断
        switch (type){
            //相互喜欢
            case 1://根据用户id分页条件查询好友关系
                Query query = new Query(Criteria.where("userId").is(userId))
                        .skip(index).limit(pageSize);
                List<Friend> friends = mongoTemplate.find(query, Friend.class);
                //判断非空 遍历
                if (CollectionUtil.isNotEmpty(friends)) {
                    for (Friend friend : friends) {
                        //创建vo类 赋值并添加到vo集合
                        UserLikeVo userLikeVo = new UserLikeVo();
                        userLikeVo.setId(friend.getFriendId());
                        userLikeVos.add(userLikeVo);
                    }
                }//调用rpc查询相互喜欢人数
                long count = mongoTemplate.count(query, Friend.class);
                //返回
                return new PageBeanVo(pageNum,pageSize,count,userLikeVos);
                //喜欢
            case 2:
                //根据用户id分页条件查询用户喜欢集合
                Query query1=new Query(Criteria.where("userId").is(userId))
                        .skip(index).limit(pageSize);
                List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
                //判断非空 遍历
                if (CollectionUtil.isNotEmpty(userLikes)) {
                    for (UserLike userLike : userLikes) {
                        //创建vo类 赋值并保存到vo集合
                        UserLikeVo userLikeVo = new UserLikeVo();
                        userLikeVo.setId(userLike.getLikeUserId());
                        userLikeVos.add(userLikeVo);
                    }
                }
                //调用mongo查询喜欢数量
                long count1 = mongoTemplate.count(query1, UserLike.class);
                //返回
                return new PageBeanVo(pageNum,pageSize,count1,userLikeVos);
                //粉丝
            case 3:
                //创建条件为被喜欢id是用户id的用户喜欢集合
                Query query2=new Query(Criteria.where("likeUserId").is(userId))
                        .skip(index).limit(pageSize);
                List<UserLike> userLikes1 = mongoTemplate.find(query2, UserLike.class);
                //判断非空 遍历
                if (CollectionUtil.isNotEmpty(userLikes1)) {
                    for (UserLike userLike : userLikes1) {
                        //创建vo类 赋值并保存到vo集合
                        UserLikeVo userLikeVo = new UserLikeVo();
                        userLikeVo.setId(userLike.getUserId());
                        userLikeVos.add(userLikeVo);
                    }
                }
                //调用mongo查询粉丝数量
                long count2 = mongoTemplate.count(query2, UserLike.class);
                //返回
                return new PageBeanVo(pageNum,pageSize,count2,userLikeVos);
                //谁看过我
            case 4:
                //创建条件为用户id相同 按照访问时间降序 并分页 查询出访客集合
                Query query3=new Query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("date")))
                        .skip(index).limit(pageSize);
                List<Visitor> visitors = mongoTemplate.find(query3, Visitor.class);
                //判断非空遍历
                if (CollectionUtil.isNotEmpty(visitors)) {
                    for (Visitor visitor : visitors) {
                        //创建vo类 赋值并保持vo集合
                        UserLikeVo userLikeVo = new UserLikeVo();
                        userLikeVo.setId(visitor.getVisitorUserId());
                        userLikeVo.setMatchRate(visitor.getScore().intValue());
                        userLikeVos.add(userLikeVo);
                    }
                }
                //调用mongo查询谁看过我数量
                long count3 = mongoTemplate.count(query3, Visitor.class);
                //返回
                return new PageBeanVo(pageNum,pageSize,count3,userLikeVos);
        }
        return null;
    }
}
